package com.wry.controller;

import com.alibaba.fastjson.JSON;
import com.wry.model.Order;
import com.wry.service.OrderService;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.IndexOperations;
import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <h1> ES基本操作 <h1>
 *
 * @author wry
 * @since 2023/8/22 19:41
 */
@RequestMapping("es")
@RestController
public class EsController {

    @Resource
    @Lazy
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Resource
    private OrderService orderService;


    /**
     * 创建索引
     *
     * @param indexName 索引名称
     * @return
     */
    @GetMapping("index/create")
    public String createIndex(@RequestParam String indexName) {
        IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(IndexCoordinates.of(indexName));
        if (indexOperations.exists()) {
            return "索引已存在";
        }
        indexOperations.create();
        return "索引创建成功";
    }

    /**
     * 删除索引
     */
    @GetMapping("index/del")
    public String create(@RequestParam String indexName) {
        final IndexOperations indexOperations = elasticsearchRestTemplate.indexOps(IndexCoordinates.of(indexName));
        final boolean delete = indexOperations.delete();
        return "删除索引->" + delete;
    }

    /**
     * 批量创建文档
     *
     * @param orderList 文档集合
     * @return
     */
    @PostMapping("doc/save-batch")
    public String saveBatch(@RequestBody List<Order> orderList){
        if (orderList.isEmpty()){
            return "文档不能为空！";
        }
        orderService.saveAll(orderList);
        return "ok";
    }

    /**
     * 根据id删除文档
     *
     * @param id order的id
     * @return
     */
    @DeleteMapping("doc/del/{id}")
    public String del(@PathVariable Integer id){
        orderService.deleteById(id);
        return "ok";
    }

    /**
     * 根据id更新文档
     *
     * @param order 实体
     * @return
     */
    @PutMapping("doc/update")
    public String update(@RequestBody Order order){
        orderService.updateById(order);
        return "ok";
    }

    /**
     * 根据id查询文档
     *
     * @param id order的id
     * @return
     */
    @GetMapping("doc/{id}")
    public String update(@PathVariable Integer id){
        return JSON.toJSONString(orderService.findById(id));
    }

    /**
     * 分页查询，不带条件
     * @param pageIndex 页码
     * @param pageSize 页大小
     * @return
     */
    @GetMapping("doc/find-all")
    public String saveBatch(@RequestParam Integer pageIndex, @RequestParam Integer pageSize){
        return JSON.toJSONString(orderService.findAll(pageIndex, pageSize));
    }

    /**
     * 条件分页搜索
     */
    @GetMapping("doc/findList")
    public String findList(@RequestBody Order order, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
        return JSON.toJSONString(orderService.findList(order, pageIndex, pageSize));
    }

    /**
     * 根据username模糊匹配
     */
    @GetMapping("doc/find-by-name")
    public String findByName(@RequestParam String username, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
        return JSON.toJSONString(orderService.findListByName(username, pageIndex, pageSize));
    }

    /**
     * 条件高亮分页搜索
     */
    @GetMapping("doc/findHighlight")
    public String findHighlight(@RequestBody(required = false) Order order, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
        return JSON.toJSONString(orderService.findHighlight(order, pageIndex, pageSize));
    }

    /**
     * 范围查询
     */
    @GetMapping("doc/range")
    public String findRange(@RequestParam Long startNo, @RequestParam Long endNo, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
        return JSON.toJSONString(orderService.rangeQueryByOrderNo(startNo,endNo, pageIndex, pageSize));
    }

    /**
     * 日期范围查询
     */
    @GetMapping("doc/range-time")
    public String findRangeTime(@RequestParam String startTime, @RequestParam String endTime, @RequestParam Integer pageIndex, @RequestParam Integer pageSize) {
        return JSON.toJSONString(orderService.rangeQueryByTime(startTime,endTime, pageIndex, pageSize));
    }

}
